package com.yiren.dbaa.module.help_center.view;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yiren.dbaa.R;
import com.yiren.dbaa.constant.StringConstant;
import com.yiren.dbaa.net.oss.IOssFileOperationApi;
import com.yiren.dbaa.net.oss.impl.OssFileOperationApiImpl;
import com.yiren.dbaa.module.help_center.bean.ToolkitBean;
import com.yiren.dbaa.module.help_center.contract.ToolkitContract;
import com.yiren.dbaa.module.help_center.presenter.ToolkitPresenter;
import com.yiren.dbaa.util.NoDoubleClickUtil;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Describe
 * 工具包适配器
 */
public class ToolkitAdapter extends BaseAdapter implements ToolkitContract.View {

    private Activity activity;
    private List<ToolkitBean> toolkitBeanList;
    private ToolkitContract.Presenter toolkitPresenter;
    private IOssFileOperationApi ossFileOperationApi;
    private Long total;
    private int sumProgressBarPosition = 0; //进度条累计进度
    private DecimalFormat df = new DecimalFormat("######0.00"); //百分比格式化工具

    private int selectPosition;

    public ToolkitAdapter(Activity activity, List<ToolkitBean> toolkitBeanList) {
        this.activity = activity;
        this.toolkitBeanList = toolkitBeanList;
        toolkitPresenter = new ToolkitPresenter(activity);
        ossFileOperationApi = new OssFileOperationApiImpl(activity);
    }

    @Override
    public int getCount() {
        return toolkitBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return toolkitBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.item_toolkit, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        mHolder.tvToolkitName.setText(toolkitBeanList.get(position).getName());
        mHolder.tvToolkitDown.setText(toolkitBeanList.get(position).getBtName());
        mHolder.tvToolkitDown.setOnClickListener(v -> {
            if (NoDoubleClickUtil.isDoubleClick()) {
                return;
            }
            selectPosition = position;
            //下载 
            if (!toolkitBeanList.get(position).isDown()) {
                toolkitPresenter.downLoadTookit(this, toolkitBeanList.get(position));
            } else {
                toolkitPresenter.installToolkit(StringConstant.APK_NAME);
            }
        });

        return convertView;
    }

    @Override
    public void showToolkit(List<ToolkitBean> toolkitBeanList) {

    }

    @Override
    public void showProgress(String len) {
        toolkitBeanList.get(selectPosition).setBtName(len);
        if (len.equals("100.00%")) {
            toolkitBeanList.get(selectPosition).setBtName("安装");
        }
        activity.runOnUiThread(() -> notifyDataSetChanged());
    }

    @Override
    public void downSuccess() {
        toolkitBeanList.get(selectPosition).setDown(true);
        toolkitBeanList.get(selectPosition).setBtName("安装");
        activity.runOnUiThread(() -> notifyDataSetChanged());
    }

    class ViewHolder {
        TextView tvToolkitName;
        TextView tvToolkitDown;

        public ViewHolder(View itemView) {
            tvToolkitName = (TextView) itemView.findViewById((R.id.tv_title));
            tvToolkitDown = (TextView) itemView.findViewById((R.id.bt_toolkit_down));
        }
    }

}
